package com.chinadci.smartcommunitysystembackstage.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.chinadci.smartcommunitysystembackstage.entity.ScsOwner;
import com.baomidou.mybatisplus.extension.service.IService;
import com.chinadci.smartcommunitysystembackstage.entity.ScsUser;
import com.chinadci.smartcommunitysystembackstage.entity.Yz;
import com.chinadci.smartcommunitysystembackstage.mapper.ScsAdmintypeMapper;
import com.chinadci.smartcommunitysystembackstage.mapper.ScsOwnerMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务类
 * </p>
 *
 * @author Code Duck
 * @since 2021-04-13
 */
@Service
public class ScsOwnerService{
    @Autowired
    private ScsOwnerMapper mapper;

    private Logger log= LoggerFactory.getLogger(this.getClass());

    //显示全部业主信息
    public Map Find(String name){
        log.info("=====显示全部业主信息");
        String sql="SELECT o_id,o_name,o_sex,o_age,o_parent,o_number,o_phone,o_company,t_notypename,n_noname,h_floor,h_no FROM scs_owner ow \n" +
                "INNER JOIN scs_house ho \n" +
                "INNER JOIN scs_house_no n\n" +
                "INNER JOIN scs_house_notype ty\n" +
                "INNER JOIN scs_house_unit u\n" +
                "ON (ow.h_id=ho.h_id)\n" +
                "AND (ho.n_id=n.n_id)\n" +
                "AND (ho.u_id=u.u_id)\n" +
                "AND (ho.t_id=ty.t_id)";
        if (name!=null & name!=""){
            sql+=" and o_name like"+"'%"+name+"%'";
        }
        Map<String,Object> map=new HashMap<>();
        map.put("data",mapper.Selects(sql));
        return map;
    }

    //添加业主
    public int Insert(Yz user){
        log.info("=====添加业主信息");
        Map<String,Object> map=new HashMap<>();
        map.put("oname",user.getOname());
        map.put("osex",user.getOsex());
        map.put("oage",user.getOage());
        map.put("oparent",user.getOparent());
        map.put("onumber",user.getOnumber());
        map.put("ophone",user.getOphone());
        map.put("ocompany",user.getOcompany());
        map.put("nid",user.getNid());
        map.put("uid",user.getUid());
        map.put("hno",user.getHno());
        map.put("hsize",user.getHsize());
        map.put("hfloor",user.getHfloor());
        map.put("tid",user.getTid());
        mapper.Insert(map);
        int result=Integer.parseInt(map.get("result").toString());
        return result;
    }

    //修改的时候查询
    public ScsOwner Find(int id){
        log.info("=====修改的时候查询业主信息");
        QueryWrapper<ScsOwner> qw=new QueryWrapper<>();
        qw.eq("o_id",id);
        return mapper.selectOne(qw);
    }

    //修改
    public int Update(ScsOwner scs){
        log.info("=====修改业主信息");
        return mapper.updateById(scs);
    }


    //删除用户
    public int Del(int id){
        log.info("=====删除业主信息");
        Map<String,Object> map=new HashMap<>();
        map.put("tid",id);
        mapper.Del(map);
        int result=Integer.parseInt(map.get("result").toString());
        return result;
    }

    //根据电话号查找
    public ScsOwner Finds(String phone){
        log.info("=====新增用户账号信息的时候根据手机号判断该用户是否在本小区住");
        QueryWrapper<ScsOwner> qw=new QueryWrapper<>();
        qw.eq("o_phone",phone);
        return mapper.selectOne(qw);
    }

    //根据电话号查找id
    public ScsOwner Findss(String phone){
        log.info("=====新增用户账号信息的时候根据手机号查找用户信息编号");
        QueryWrapper<ScsOwner> qw=new QueryWrapper<>();
        qw.eq("o_phone",phone);
        return mapper.selectOne(qw);
    }

    //根据用户名查找编号
    public ScsOwner finds(String name){
        log.info("=====根据用户名查找编号");
        QueryWrapper<ScsOwner> qw=new QueryWrapper<>();
        qw.eq("o_name",name);
        return mapper.selectOne(qw);
    }

}